<?php

namespace App\Http\Controllers\App\V1;

use Carbon\Carbon;
use App\Response\Response;
use Illuminate\Http\Request;
use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;

use function AlibabaCloud\Client\json;

class WeatherController extends BaseController
{

    public function show(Request $request)
    {
        
        $ip = $request->input('ip') ?? get_real_ip();
        if (!$ip) {
            throw new BasicException(0, 'ip有误');
        }

        $redis_key = 'weather:ip:'.$ip;
        if (app('redis')->exists($redis_key)) {
            $result = app('redis')->get($redis_key);
            return Response::sendData(json_decode($result, true));
        }

        header("Content-Type:text/html;charset=UTF-8");
        date_default_timezone_set("PRC");
        $host = "https://weather01.market.alicloudapi.com/ip-to-weather";
        $method = "GET";
        $appcode = env('MARKET_APP_CODE');
        $headers = [];
        array_push($headers, "Authorization:APPCODE ".$appcode);
        array_push($headers, "Content-Type: application/x-www-form-urlencoded; charset=utf-8");
        $querys = "ip=".$ip."&need3HourForcast=0&needAlarm=0&needHourData=0&needIndex=0&needMoreDay=0";
        $bodys = "";
        $url = $host."?".$querys;
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, true);
        if (1 == strpos("$".$host, "https://")) {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);
        $response = curl_exec($curl);
        $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
        $headers = substr($response, 0, $header_size);
        
        $body = substr($response, $header_size);
        $result = json_decode($body, true);
        if (!$result) {
            throw new BasicException(0, '查询有误');
        }
        if(isset($result['showapi_res_body']['ret_code']) && $result['showapi_res_body']['ret_code'] == -1){
            $msg = $result['showapi_res_body']['remark']??'查询错误';
            throw new BasicException(0, $msg);
        }
        $return_data = [
            'city'        => isset($result['showapi_res_body']['cityInfo']['c4']) ? strtoupper($result['showapi_res_body']['cityInfo']['c4']) : '',
            'temperature' => $result['showapi_res_body']['now']['temperature'] ?? '',
            'weather'     => $result['showapi_res_body']['now']['weather'] ?? '',
            'date'        => strtoupper(Carbon::now()->format('Y.m.d D')),
        ];

        app('redis')->setex($redis_key, 21600, json_encode($return_data));

        return Response::sendData($return_data);
    }
}
